package cn.linkai.hadoop.mydefine;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class TradeBean implements WritableComparable<TradeBean> {

    private Text userName;
    private DoubleWritable income;
    private DoubleWritable outcome;
    private DoubleWritable profit;

    public TradeBean() {
        this.userName=new Text();
        this.income=new DoubleWritable(0);
        this.outcome=new DoubleWritable(0);
        this.profit=new DoubleWritable(0);
    }

    public void set(Text userName,DoubleWritable income,DoubleWritable outcome){
        this.userName.set(userName);
        this.income.set(income.get());
        this.outcome.set(outcome.get());
        this.profit.set(this.income.get()-this.outcome.get());
    }

    public DoubleWritable getIncome() {
        return income;
    }

    public DoubleWritable getOutcome() {
        return outcome;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        userName.write(dataOutput);
        income.write(dataOutput);
        outcome.write(dataOutput);
        profit.write(dataOutput);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        userName.readFields(dataInput);
        income.readFields(dataInput);
        outcome.readFields(dataInput);
        profit.readFields(dataInput);
    }

    @Override
    public int compareTo(TradeBean o) {
        if(profit.get()>o.profit.get()){
            return -1;
        }else if((profit.get()==o.profit.get())&&income.get()>o.income.get()){
            return -1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return super.toString()+" "+userName+"\t"
                 + income+"\t"
                + outcome+"\t"
               + profit
               ;
    }
}
